package com.example.jbmsuppliers.sinopec.doorjurisd;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.jbm.jbmsupplier.util.IEGuardKey;

import java.util.Calendar;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class DistributeOneActivity extends ActivityUse {
    @InjectView(R.id.id_distribute_mobile)
    EditText idDistributeMobile;
    @InjectView(R.id.id_distribute_cade)
    EditText idDistributeCade;

    @InjectView(R.id.id_distribute_name)
    EditText idDistributeName;

    @InjectView(R.id.header_title)
    TextView headerTitle;
    @InjectView(R.id.header_right)
    TextView headerRight;
    @InjectView(R.id.header_back)
    ImageView headerBack;
    @InjectView(R.id.texttime)
    TextView texttime;
    @InjectView(R.id.teviewshowtime)
    TextView teviewshowtime;
    private int mYear;
    private int mMonth;
    private int mDay;
    static final int DATE_DIALOG = 0;

    private String endtime, startime;
    private DialogLoading dialog;
    private final int MSG_CODE = 0001;
    public static final int Cmd_ShareEGuardKeys = 0002;

    @Override
    protected int setContentView1() {
        return R.layout.activity_distribute_one;
    }

    @Override
    protected void initView() {
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);
        headerTitle.setText("分发授权");
        headerRight.setText("完成");
        texttime.setText(getTime());
    }




    private Handler mHandler = new Handler() {
        public void handleMessage(Message paramMessage) {
            switch (paramMessage.what) {
                case Cmd_ShareEGuardKeys:
                    IEGuardKey key = (IEGuardKey) paramMessage.obj;
                    if (key == null || !key.back) {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        hint("分发失败");
                        return;
                    }
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                    hint("分发成功", mHandler, MSG_CODE);
                    break;
                case MSG_CODE:
                    finish();
                    break;
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1:
                    this.setResult(Activity.RESULT_OK);
                    this.finish();
                    break;
            }
        }
    }


    //日期
    private void ShowDateDialog() {
        final Calendar c = Calendar.getInstance();
        new DatePickerDialog(this, mdatesetlistener, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
    }

    private DatePickerDialog.OnDateSetListener mdatesetlistener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

            mYear = year;
            mMonth = month;
            mDay = dayOfMonth;
            updateDisplay();
        }
    };

    //重写diallog方法是用来回掉上面的监听
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG:
                return new DatePickerDialog(this, mdatesetlistener, mYear, mMonth, mDay);
        }
        return null;
    }

    //将日期设置在需要的控件上
    private void updateDisplay() {
        texttime.setText(new StringBuilder().append(mYear).append("-").append(mMonth + 1).append("-").append(mDay).append(""));

    }

    @OnClick({R.id.header_right, R.id.header_back, R.id.teviewshowtime})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_right:
                String mobile = idDistributeMobile.getText().toString();
                endtime = texttime.getText().toString();
                startime = getTime();
                // int end = new StringBuffer().append(endtime.substring(0, endtime.indexOf("-"))).append(endtime.substring(endtime.indexOf("-")+1,endtime.lastIndexOf("-"))).append(endtime.substring(endtime.lastIndexOf("-")+1));
//                StringBuffer star = new StringBuffer().append(startime.substring(0, startime.indexOf("-"))).append(startime.substring(startime.indexOf("-")+1,startime.lastIndexOf("-"))).append(startime.substring(startime.lastIndexOf("-")+1));
             int    end=  Integer.parseInt( new StringBuffer().append(endtime.substring(0, endtime.indexOf("-"))).append(endtime.substring(endtime.indexOf("-")+1,endtime.lastIndexOf("-"))).append(endtime.substring(endtime.lastIndexOf("-")+1))+"");
                int star=Integer.parseInt(new StringBuffer().append(startime.substring(0, startime.indexOf("-"))).append(startime.substring(startime.indexOf("-")+1,startime.lastIndexOf("-"))).append(startime.substring(startime.lastIndexOf("-")+1))+"");

                int a = endtime.compareTo(startime);
                if (mobile.equals("")) {
                    hint("请输入手机号！");
                    return;
                } else if (mobile.length() != 11) {
                    hint("请输入正确手机号！");
                    return;
                } else if (getTime().equals(texttime.getText().toString())) {
                    hint("请选择分发有效日期！");
                    return;
                } else if (/*a < 0 || a < -1*/end<star) {
                    hint("请选择今天以后的日期！");
                    return;
                }else {
                    NetWork netWork = new NetWork(mHandler);
                    netWork.ShareEGuardKey(getIntent().getIntExtra("egkey_id", 1), -1, getCurrentDay(), texttime.getText().toString() + " " + getDateTime(), mobile);
                    dialog = new DialogLoading(DistributeOneActivity.this, "分发中...", true);
                    dialog.show();
                }


//                else if (idDistributeName.getText().toString().equals("")) {
//
//                    hint("姓名不能为空！");
//                } else if (idDistributeCade.getText().toString().equals("")) {
//                    hint("请输入个人信息！");
//                } else if (idDistributeCade.getText().toString().length() != 18) {
//                    hint("请核实您的身份证件！");
//                } else  {
//                    try {
//                        if (!IdCardDetection.IDCardValidate(idDistributeCade.getText().toString()).equals("")) {
////                            hint("请核实您的身份证件！");
//                        } else {
//                            NetWork netWork = new NetWork(mHandler);
//                            netWork.ShareEGuardKey(getIntent().getIntExtra("egkey_id", 1), -1, getCurrentDay(), texttime.getText().toString() + " " + getDateTime(), mobile, idDistributeName.getText().toString(), idDistributeCade.getText().toString());
//                            dialog = new DialogLoading(DistributeOneActivity.this, "分发中...", true);
//                            dialog.show();
//                        }
//                    } catch (ParseException e) {
//                        e.printStackTrace();
//                    }
                    break;

//        }
            case R.id.header_back:
                finish();
                break;
            case R.id.teviewshowtime:
                ShowDateDialog();
                break;
        }
    }


}